package com.pswv2.backstage.category.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pswv2.backstage.category.servic.CategoryService;
import com.pswv2.bub.bsc.dao.po.Category;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;
import com.pswv2.util.Paging;
import com.pswv2.util.Strings;
/**
 * 分类操作
 * @author linjiang
 *
 */
@Controller
@RequestMapping(value = "categoryAcion", produces = { "text/json;charset=UTF-8" })
public class CategoryAction {
	private static Class<?> poc = Category.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private CategoryService categoryService;

	/**
	 * 添加
	 */
	@ResponseBody
	@RequestMapping(value = { "addCategory" })
	public String add(String name) {
		Category category = new Category();
		category.setName(name);
		categoryService.add(category);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = { "listAll" })
	public String list(HttpSession session) {
		List<Category> allcategory =  categoryService.queryAll();
		return CommonResp.buildSuccessResp(allcategory).toJsonString();
	}
	/**
	 * 查看所有
	 */
	 
	@ResponseBody
	@RequestMapping(value = { "list" })
	public String list(int pageIndex, int pageSize, HttpSession session) {
		pageIndex=pageIndex*pageSize;
		Paging<?> paging = extraSpringHibernateTemplate.findPage(poc, Order.desc("categoryId"),pageIndex, pageSize);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	/**
	 * 通过Id查询
	 */
	
	@ResponseBody
	@RequestMapping(value = { "queryById" })
	public String queryById(int id) {
		Category category = categoryService.queryById(id);
		if (category == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		return CommonResp.buildSuccessResp(category).toJsonString();
	}

	/**
	 * 修改
	 */
		
	@ResponseBody
	@RequestMapping(value = { "edit" })
	public String edit(Integer id, String name,  HttpSession session) {
		Category toUpdate = categoryService.queryById(id);
		if (toUpdate == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
	if (StringUtils.isNoneBlank(name)) {
			toUpdate.setName(Strings.trimAndNotNull(name));
		}
		extraSpringHibernateTemplate.getHibernateTemplate().update(toUpdate);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
	

	/**
	 * 删除
	 */
	
	@ResponseBody
	@RequestMapping(value = { "remove" })
	public String remove(int id) {
		Category category = categoryService.queryById(id);
		if (category == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(category);
		return CommonResp.buildSuccessResp().toJsonString();
	}
		
	/**
	 * 条件查询
	 */

	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String name, HttpSession session) throws ParseException {
		
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(name)) {
			criterions.add(Restrictions.ilike("name", "%" + name + "%"));
		}
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("categoryId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
	
}
